package com.springboot.chapter3.config;

import com.springboot.chapter3.condition.DatabaseConditional;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 作者：paopao
 * 版本：1.0
 */
@Configuration
@ComponentScan(value = "com.springboot.chapter3.*",
////    excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Service.class)},
    lazyInit = true) // 标明进行扫描，排除扫描UserService类
@ImportResource(value = {"classpath:spring-other.xml"})
public class AppConfig {

//    @Bean(name = "user")
//    public User user(){
//        User user = new User();
//        user.setId(1L);
//        user.setUserName("paopao");
//        user.setNote("this is paopao");
//        return user;
//    }

    // 配置数据源，属于自定义配置的Bean
//    @Bean(name = "dataSource")
//    public DataSource getDataSource(){
//        Properties properties = new Properties();
//        properties.setProperty("driver", "com.mysql.jdbc.Driver");
//        properties.setProperty("url", "jdbc:mysql://localhost:3306/paopao");
//        properties.setProperty("username", "root");
//        properties.setProperty("password", "123456");
//        DataSource dataSource = null;
//        try {
//            dataSource = BasicDataSourceFactory.createDataSource(properties);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return dataSource;
//    }

//    @Bean(name = "dataSource")
//    @Conditional(DatabaseConditional.class)
//    public DataSource getDataSource(){
//        Properties properties = new Properties();
//        properties.setProperty("driver", "com.mysql.jdbc.Driver");
//        properties.setProperty("url", "jdbc:mysql://localhost:3306/paopao");
//        properties.setProperty("username", "root");
//        properties.setProperty("password", "123456");
//        DataSource dataSource = null;
//        try {
//            dataSource = BasicDataSourceFactory.createDataSource(properties);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return dataSource;
//    }

    @Bean(name = "dataSource")
    @Profile("dev")
    public DataSource getDevDataSource(){
        Properties properties = new Properties();
        properties.setProperty("driver", "com.mysql.jdbc.Driver");
        properties.setProperty("url", "jdbc:mysql://localhost:3306/dev");
        properties.setProperty("username", "root");
        properties.setProperty("password", "123456");
        DataSource dataSource = null;
        try {
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    @Bean(name = "dataSource")
    @Profile("value = test")
    public DataSource getTestDataSource(){
        Properties properties = new Properties();
        properties.setProperty("driver", "com.mysql.jdbc.Driver");
        properties.setProperty("url", "jdbc:mysql://localhost:3306/test");
        properties.setProperty("username", "root");
        properties.setProperty("password", "123456");
        DataSource dataSource = null;
        try {
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}
